/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mastercard.assets.web.struts.forms;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import com.mastercard.assets.jpa.TblCuenta;
import com.mastercard.assets.jpa.TblRole;
import com.mastercard.assets.web.util.to.UserLogin;

/**
 *
 * @author rod
 */
public class LoginGanadorForm
        extends
                ActionForm
{
        private String user ;
	private String pass ;
	
	public String getUser( )
	{
		return user ;
	}

	public void setUser( String user )
	{
		this.user = user ;
	}

	public String getPass( )
	{
		return pass ;
	}
	
	public void setPass( String pass )
	{
		this.pass = pass ;
	}
        
        public ActionErrors validate( ActionMapping mapping , HttpServletRequest request )
	{
		ActionErrors errors = new ActionErrors( ) ;
		
		// verifica que no esten vacios los campos
		if( user == null || user.trim().length() == 0 )
			// agrega el error
			errors.add( "LoginEmpty" , new ActionMessage( "com.mastercard.assets.web.errors.modules.login.LoginEmpty" ) ) ;
		// verifica que no esten vacios los campos
		if( pass == null || pass.trim().length() == 0 )
			// agrega el error
			errors.add( "PasswordEmpty" , new ActionMessage( "com.mastercard.assets.web.errors.modules.login.PasswordEmpty" ) ) ;
		
		if( errors.size( ) > 0 )
			return errors ;
		
		try
		{
			// crea el usuario a manejar
			UserLogin userTO = new UserLogin( user , pass ) ;
			
			boolean res = userTO.loginGanador( ) ;
			
			// en caso de ser false
			if( ! res )
				// agrega el error
				errors.add( "LoginNotCheck" , new ActionMessage( "com.mastercard.assets.web.errors.modules.login.LoginNotCheck" ) ) ;
			// en caso de que si se haya logueado
			else
			{
                            HttpSession session = request.getSession( true ) ;
                            // guarda el objeto del UserLogin
                            session.setAttribute( "UserLogin" , userTO ) ;				
			}
		}catch( Exception e )
		{
			errors.add( "LoginException" , new ActionMessage( "com.mastercard.assets.web.errors.modules.login.exception" , new Object[ ]{ e.getMessage( ) } ) ) ;
		}
		
		return errors ;
	}
    
}
